// pages/device/state/state.js

var app = getApp()
const bluetooth = require('../../../command/bluetooth.js')
var cmd = require('../../../command/command.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        showBind: true
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var result = app.checkIsBindedByNameOfInfo(app.globalData.bluetoothName)
        console.log(result)
        if(result == 0){
            // 未绑定
            this.setData({
                showBind : true,
              })
        }
        else if(result == 2){
            // 已绑定
            this.setData({
                showBind : false,
              })
        }
        else {
            console.error(app.globalData.bluetoothName, "已被其他用户绑定")
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        console.log("state.js onHide()")
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
    },
    goReportError() {
        if (getCurrentPages().length > 8) {
            wx.reLaunch({
                url: '../report/report',
            })

        }
        else {
            wx.navigateTo({
                url: '../report/report',
            })
        }
    },
    goCallPhone() {
        console.log("call phone")

        if (app.globalData.phoneSystem == "android") {
            console.log("android")
            wx.showActionSheet({
                itemList: ['400-1380-026', '呼叫'],
                success: function (res) {
                    if (res.tapIndex == 1) {
                        wx.makePhoneCall({
                            phoneNumber: '400-1380-026',
                            success: function (res) {
                                console.log(res)
                            },
                            fail: function (res) {
                                console.error(res)
                            }
                        })
                    }
                }
            })
        } else if (app.globalData.phoneSystem == "ios") {
            console.log("ios")
            wx.makePhoneCall({
                phoneNumber: '400-1380-026',
                success: function (res) {
                    console.log(res)
                },
                fail: function (res) {
                    console.error(res)
                }
            })
        }
    },
    goToMachine() {
        wx.redirectTo({
            url: "../home/home",
        })
    },
    goUpdatePage() {
        // 每次都下载最新的更新包
        app.getUpdateInfo()

        if (cmd.getMachineVersion() == null) {
            console.warn("未获取到固件版本号")
            bluetooth.send(cmd.getCommandByName("appVersion"))
            setTimeout(() => {
                console.log("timeout---固定")
                this.update()
            }, 100);
        } else {
            this.update()
        }

    },

    update() {
        //检查更新
        if (cmd.getMachineVersion() == null) {
            wx.showToast({
                title: '无设备版本号！',
                icon: 'error'
            })
            return
        }

        if (app.globalData.updateFilePath == "") {
            wx.showToast({
                title: '更新文件不存在',
                icon: 'error'
            })
            return
        }

        if (cmd.getMachineVersion() != app.globalData.newVersion) {
            wx.showModal({
                title: '检查更新',
                content: '检查到有新固件版本，是否更新？',
                complete: (res) => {
                    if (res.cancel) {

                    }

                    if (res.confirm) {
                        console.log("current-Page-Length:", getCurrentPages().length)
                        if (getCurrentPages().length > 8) {
                            wx.reLaunch({
                                url: '../update/update',
                            })

                        }
                        else {
                            wx.navigateTo({
                                url: '../update/update',
                            })
                        }
                    }
                }
            })
        } else {
            wx.showToast({
                title: '无需更新！',
            })
        }
    },

    bindUser() {
        /* 
        绑定：https://cqygdqkj.top/api/v1/zqwb/user_bind_info/
        接口方法：POST
        参数: {
            is_save: 是否保存： 默认False:不保存
            machine_info: 设备名称
            user: 当前用户传递id
            }
        */

        wx.request({
            url: 'https://cqygdqkj.top/api/v1/zqwb/user_bind_info/',
            method: "POST",
            header: {
                "Authorization": app.globalData.headerToken,
            },
            data: {
                "machine_info": app.globalData.bluetoothName,
            },
            success: (res) => {
                console.log(res, res.data.code)
                cmd.saveSendMessageText("bindUser", res)
                if(res.data.code == 202){
                    wx.showToast({
                        title: '请等待审核',
                    })
                }else{
                    wx.showToast({
                        title: '已提交申请',
                    })
                }
            },
            fail: (res) => {
                console.log(res)
                cmd.saveSendMessageText("bindUser", res)
                wx.showToast({
                    title: '提交申请失败',
                    icon: 'error'
                })
            },
        })
    },
    unbindUser() {
        wx.request({
            url: 'https://cqygdqkj.top/api/v1/zqwb/unbind_info/',
            method: "POST",
            header: {
                "Authorization": app.globalData.headerToken,
            },
            data: {
                "id": app.globalData.userId,
            },
            success: (res) => {
                console.log(res)
                cmd.saveSendMessageText("unbindUser", res)
                wx.showToast({
                    title: '已解绑',
                })

                this.setData({
                    showBind : true,
                  })
            },
            fail: (res) => {
                console.log(res)
                cmd.saveSendMessageText("unbindUser", res)
                wx.showToast({
                    title: '解绑失败',
                    icon: 'error'
                })
            },
        })
    }
})